//
//  CustomVolumeControlView.m
//  VideoPlayerExample
//
//  Created by Lin Eidan on 13-7-30.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "CustomVolumeControlView.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import "UIView+EidanSize.h"
#import "Common.h"

#define WidthPer       0.9
#define VolumeChange  @"volumeChange"

@implementation CustomVolumeControlView

- (id)initWithFrame:(CGRect)frame andDelegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate=delegate;
        
        self.backgroundColor=[UIColor clearColor];
        UIView *bgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        bgView.backgroundColor=[UIColor blackColor];
        bgView.alpha=0.5;
        [self addSubview:bgView];
        
        [self setUp];
        [self addHardKeyVolumeListener];
    }
    return self;
}


-(void)setUp{
//    self.backgroundColor=[UIColor purpleColor];
//    MPVolumeView *mpVolumeView=[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
//    mpVolumeView.backgroundColor=[UIColor greenColor];
//    [self addSubview:mpVolumeView];
    
//    self.muteBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    self.muteBtn.backgroundColor=[UIColor lightGrayColor];
//    [self.muteBtn setBackgroundImage:[UIImage imageNamed:@"percent_bg.png"] forState:UIControlStateNormal];  //有音量状态的图片
//    [self.muteBtn setBackgroundImage:[UIImage imageNamed:@"percent_bar.png"] forState:UIControlStateSelected];  //静音的时候的图片，变为选中状态
//    [self.muteBtn addTarget:self action:@selector(setUpMute) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:self.muteBtn];
    
    
    [self handleMuteBtn:[[MPMusicPlayerController applicationMusicPlayer] volume]];
    
    self.volumeSlider=[[SliderControlView alloc] initWithFrame:CGRectMake((self.width*(1-WidthPer))/2, 0, self.width*WidthPer, self.height) andMinValue:0 andLeftBackgroundImageName:@"mov_iconbutton2.png" andRightBg:@"mov_iconbutton1.png" andSliderBtnImageName:@"mov_iconbutton3.png"  andLeftCapWidth:9 andTopCapHight:2.5 andPopImageName:nil andPopViewMove:0 andSliderLabelDisplay:0];
    
//    self.volumeSlider.backgroundColor=[UIColor purpleColor];
    [self.volumeSlider SliderControlSetUpMaxValue:1];
    [self addSubview:self.volumeSlider];
    self.volumeSlider.delegate=self;
    [self.volumeSlider SliderControlSetUpToValue:[[MPMusicPlayerController applicationMusicPlayer] volume]];
    
    NSNotificationCenter *notiCenter=[NSNotificationCenter defaultCenter];  //增加一个观察者，观察音量是否改变，收到通知就调用updateTheSliderView函数
    [notiCenter addObserver:self selector:@selector(updateTheSliderView:) name:VolumeChange object:nil];
    
    self.transform=CGAffineTransformMakeRotation(-M_PI * 0.5);
}

-(float)customVolumeViewGetValue;
{
    return [[MPMusicPlayerController applicationMusicPlayer] volume];
}

//外部设置音量
-(void)customVolumeViewSetValue:(float )value{
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:value];
}

//根据音量设置按钮状态
-(void)handleMuteBtn:(float) value{
//    if (value == 0) {
//        self.muteBtn.selected=YES;
//    }else{
//        self.muteBtn.selected=NO;
//    }
    BOOL isMute=YES;
    if (value == 0) {
        isMute=YES;
    }else{
        isMute=NO;
    }
    
    if ([self.delegate respondsToSelector:@selector(customVolumeViewValueChangeedAndIsMute:)]) {
        [self.delegate customVolumeViewValueChangeedAndIsMute:isMute];
    }
}

//设置静音
-(void)setUpMute{
//    [[MPMusicPlayerController applicationMusicPlayer] setVolume:0];
//    self.muteBtn.selected=YES;
}

//更新滑块位置
-(void)updateTheSliderView:(NSNotification *) notification{
    if ([[notification name] isEqualToString:VolumeChange]){
        float value=[(NSNumber *)notification.object floatValue];
        [self.volumeSlider SliderControlSetUpToValue:value];
        [self handleMuteBtn:value];
    }
}

//拖动条
-(void)SliderControlContinueToValue:(float) nums{
   
    [[MPMusicPlayerController applicationMusicPlayer] setVolume:nums];
    if ([self.delegate respondsToSelector:@selector(customVolumeViewValueChangeing:)]) {
        [self.delegate customVolumeViewValueChangeing:nums];
    }
}
//按钮抬起
-(void)SliderControlHasToValue:(float)toValue{
    if ([self.delegate respondsToSelector:@selector(customVolumeViewValueChangeedAndStop:)]) {
        [self.delegate customVolumeViewValueChangeedAndStop:toValue];
    }
}

//监听音量改变
- (void)addHardKeyVolumeListener {
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);
    AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume ,audioVolumeChangeListenerCallback,(void *)CFBridgingRetain(self));
}

void audioVolumeChangeListenerCallback(void *inUserData, AudioSessionPropertyID inPropertyID,UInt32 inPropertyValueSize, const void *inPropertyValue)
{
    if (inPropertyID != kAudioSessionProperty_CurrentHardwareOutputVolume) return;
    Float32 value = *(Float32 *)inPropertyValue;
    NSNotification *volumeMessage=[NSNotification notificationWithName:VolumeChange object:[NSNumber numberWithFloat:value]];
    NSNotificationCenter *notiCenter=[NSNotificationCenter defaultCenter];
    [notiCenter postNotification:volumeMessage];  //音量改变就发出一个通知,这样子只要有观察者在观察这个通知，他就会收到。。
}


@end
